/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.settings.device.display;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.provider.Settings;

import com.android.tv.settings.R;
import com.android.tv.settings.dialog.DialogFragment;
import com.android.tv.settings.dialog.DialogFragment.Action;
import android.os.SystemProperties;

import java.util.ArrayList;

/**
 * Activity that allows the enabling and disabling of ProjMode effects.
 */
public class DisplayModeActivity extends Activity implements Action.Listener {

    private static final String PREFERENCE_KEY   = "display";
    private static final String ACTION_WARM_TEMP = "warm_temp_0";
    private static final String ACTION_MID_TEMP  = "mid_temp_1";
    private static final String ACTION_COLD_TEMP = "cold_temp_2";
    private DialogFragment mDialogFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDialogFragment = new DialogFragment.Builder()
                .title(getString(R.string.proj_colortype))
                .breadcrumb(getString(R.string.device_display))
                .iconResourceId(getIconResource(getContentResolver()))
                .iconBackgroundColor(getResources().getColor(R.color.icon_background))
                .actions(getActions()).build();
        DialogFragment.add(getFragmentManager(), mDialogFragment);
    }

    private ArrayList<Action> getActions() {
        String TempColor = getTempColor( );
        ArrayList<Action> actions = new ArrayList<Action>();
        actions.add(new Action.Builder()
                .key(ACTION_WARM_TEMP)
                .title(getString(R.string.proj_hotcolor))
                .checked(ACTION_WARM_TEMP.contains(TempColor))
                .checkSetId(1)
                .build());
        actions.add(new Action.Builder()
                .key(ACTION_MID_TEMP)
                .title(getString(R.string.proj_midcolor))
                .checked(ACTION_MID_TEMP.contains(TempColor))
                .checkSetId(1)
                .build());
		actions.add(new Action.Builder()
                .key(ACTION_COLD_TEMP)
                .title(getString(R.string.proj_coldcolor))
                .checked(ACTION_COLD_TEMP.contains(TempColor))
                .checkSetId(1)
                .build());	
        return actions;
    }

    @Override
    public void onActionClicked(Action action) {
        if (ACTION_WARM_TEMP.equals(action.getKey())) {
        	setTempColor("0");
        } else if (ACTION_MID_TEMP.equals(action.getKey())) {
        	setTempColor("1");
        }else if (ACTION_COLD_TEMP.equals(action.getKey())) {
        	setTempColor("2");
        }
        mDialogFragment.setIcon(getIconResource(getContentResolver()));
    }

    public static String getPreferenceKey() {
        return PREFERENCE_KEY;
    }

    public static int getIconResource(ContentResolver contentResolver) {
        return R.drawable.ic_settings_display;//need to do replace ico Blues
    }

    private void setTempColor(String mValue) {
        SystemProperties.set("persist.dlp.temp", mValue);
		SystemProperties.set("persist.dlp.update", "1"); //1 is tempcolor
    }

    private static String getTempColor( ) {
        return SystemProperties.get("persist.dlp.temp", "2");
    }
	
	public static int getTempColorDesc( ) {
        String tmp =  getTempColor( );
		if (tmp.equals("0"))
		{
			return R.string.proj_hotcolor;
		}
		else if (tmp.equals("1"))
		{
			return R.string.proj_midcolor;
		}
		else if (tmp.equals("2"))
		{
			return R.string.proj_coldcolor;
		}
		return R.string.proj_coldcolor;
    }
}
